/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007, 2008 University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef TRAFFIC_SHAPER_NET_DEVICE_H
#define TRAFFIC_SHAPER_NET_DEVICE_H

#include <string.h>
#include <map>
#include "ns3/address.h"
#include "ns3/node.h"
#include "ns3/net-device.h"
#include "ns3/callback.h"
#include "ns3/packet.h"
#include "ns3/traced-callback.h"
#include "ns3/nstime.h"
#include "ns3/data-rate.h"
#include "ns3/ptr.h"
#include "ns3/mac48-address.h"
#include "ns3/drop-tail-queue.h"

namespace ns3 {


class Queue;
class PointToPointChannel;
class ErrorModel;

enum LeakState {LEAKING, NLEAKING};


class PathShaperState : public Object
{
  public:

  static TypeId GetTypeId (void);

  PathShaperState (); 
  virtual ~PathShaperState ();

  enum LeakState pathQueueState;
  Ptr<DropTailQueue> pathQueue;
  DataRate pathMaxRate;
};

/**
 * \class TrafficShaperNetDevice
 * \brief A Device for a Point to Point Network Link.
 *
 * This TrafficShaperNetDevice class specializes the NetDevice abstract
 * base class.  Together with a PointToPointChannel (and a peer 
 * TrafficShaperNetDevice), the class models, with some level of 
 * abstraction, a generic point-to-point or serial link.  
 * Key parameters or objects that can be specified for this device 
 * include a queue, data rate, and interframe transmission gap (the 
 * propagation delay is set in the PointToPointChannel).
 */
class TrafficShaperNetDevice : public PointToPointNetDevice 
{
public:
  static TypeId GetTypeId (void);

  /**
   * Construct a TrafficShaperNetDevice
   *
   * This is the constructor for the TrafficShaperNetDevice.  It takes as a
   * parameter a pointer to the Node to which this device is connected, 
   * as well as an optional DataRate object.
   */
  TrafficShaperNetDevice ();

  DataRate SetDataRate(DataRate r, int idx);
  DataRate AddPathShaper (int idx);
  /**
   * Destroy a TrafficShaperNetDevice
   *
   * This is the destructor for the TrafficShaperNetDevice.
   */
  virtual ~TrafficShaperNetDevice ();

//  void Receive (Ptr<Packet> p);


  virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
private:
  bool LeakStart(Ptr<Packet> packet, int idx);
  void LeakComplete (int idx);
 uint32_t m_bufferSize;

private:
  std::map < int, Ptr<PathShaperState> > m_pathShapersMap;  
  
};

class PathShaperTag : public Tag 
{
public:
  PathShaperTag ();
  virtual ~PathShaperTag ();
  void SetPathId (uint16_t idx)
  {
    m_pathId = idx;
  }
  uint16_t GetPathId () const
  {
    return m_pathId;
  }
private:
 uint32_t m_pathId;
public:  
  static TypeId GetTypeId (void);
  virtual TypeId GetInstanceTypeId (void) const;
  virtual void Print (std::ostream &os) const;
  virtual uint32_t GetSerializedSize (void) const;
  
  virtual void Serialize (TagBuffer start) const;
  virtual void Deserialize (TagBuffer start);

};

} // namespace ns3

#endif // TRAFFIC_SHAPER_NET_DEVICE_H

